home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
SHELL_SO
/
PARSE.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
8KB
|
408 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 012290 : Initial
*/
#include "System.h"
#include "Parse.h"
#include "Shell.h"
#define SCRIPT
/*******************************************************************/
/*
* Special Characters
*/
Boolean isPrint( char c )
{
return( isprint(c) );
}
Boolean isDigit( char c )
{
return( c >= '0' && c <= '9' );
}
Boolean isSpecial( char c )
{
switch( c )
{
case '|' :
case '&' :
case '$' :
case '<' :
case '>' :
case ';' :
case '(' :
case ')' :
case '{' :
case '}' :
case '#' :
case '\'' :
case '\"' :
return( TRUE );
default :
return( FALSE );
}
}
char spaces[ 32 ];
void getSpaces()
{
char *cp = ShellGetVar( currShell, "IFS" );
strcpy( spaces, cp );
}
Boolean isShellSpace( char c )
{
char *cp = spaces;
while( *cp )
{
if( *cp == c )
return( TRUE );
cp++;
}
return( FALSE );
}
/*******************************************************************/
/*
* Token separator
*/
char *GetShellToken( char *str, char *buf, int *type )
{
char *bufBase;
getSpaces();
bufBase = buf;
*buf = '\0';
*type = tkn_unknown;
if( str == NULL || *str == '\0' )
{
*type = tkn_eol;
return( NULL );
}
while( isShellSpace( *str ) ) /* remove white space */
str++;
switch( *str )
{
case '\r' :
case '\n' :
case '\0' :
*type = tkn_eol;
if( *str == '\0' )
return( NULL );
break;
case '|' :
*buf++ = *str++;
switch( *str )
{
case '|' :
*type = tkn_orf;
*buf++ = *str++;
break;
case '&' :
*type = tkn_pipeOutErr;
*buf++ = *str++;
break;
default :
*type = tkn_pipe;
break;
}
break;
case '&' :
*buf++ = *str++;
switch( *str )
{
case '&' :
*type = tkn_andf;
*buf++ = *str++;
break;
default :
*type = tkn_background;
break;
}
break;
case '$' :
*buf++ = *str++;
*type = tkn_var;
break;
case '<' :
*buf++ = *str++;
switch( *str )
{
case '<' :
*type = tkn_inputFromHere;
*buf++ = *str++;
break;
default :
*type = tkn_inputRedirect;
break;
}
break;
case '>' :
*buf++ = *str++;
switch( *str )
{
case '>' :
*type = tkn_outputAppend;
*buf++ = *str++;
break;
default :
*type = tkn_outputRedirect;
break;
}
break;
case ';' :
*buf++ = *str++;
switch( *str )
{
case ';' :
*type = tkn_caseDelimit;
*buf++ = *str++;
break;
default :
*type = tkn_separator;
break;
}
break;
#ifdef SCRIPT
case '(' :
*buf++ = *str++;
*type = tkn_leftParen;
break;
case ')' :
*buf++ = *str++;
*type = tkn_rightParen;
break;
case '{' :
*buf++ = *str++;
*type = tkn_leftCurl;
break;
case '}' :
*buf++ = *str++;
*type = tkn_rightCurl;
break;
case '#' :
*buf++ = *str++;
*type = tkn_comment;
break;
#endif
case '\'' :
str++; /* Move Past Quote */
while ( *str && *str != '\'' )
{
*buf++ = *str++;
}
if( *str == '\0' ) /* did not find matching quote */
{
}
*buf = '\0';
str++; /* Move Past Quote */
*type = tkn_identifier;
break;
case '\"' :
*buf++ = *str++;
*type = tkn_doubleQuote;
break;
default :
{
Boolean isNumber = TRUE;
while ( *str && !isShellSpace(*str) && !isSpecial(*str) )
{
if( !isDigit( *str ))
isNumber = FALSE;
*buf++ = *str++;
}
*buf = '\0';
#ifdef SCRIPT
if( strcmp( bufBase, "if" ) == 0 )
*type = tkn_if;
else if( strcmp( bufBase, "then" ) == 0 )
*type = tkn_then;
else if( strcmp( bufBase, "else" ) == 0 )
*type = tkn_else;
else if( strcmp( bufBase, "elif" ) == 0 )
*type = tkn_elif;
else if( strcmp( bufBase, "fi" ) == 0 )
*type = tkn_fi;
else if( strcmp( bufBase, "case" ) == 0 )
*type = tkn_case;
else if( strcmp( bufBase, "in" ) == 0 )
*type = tkn_in;
else if( strcmp( bufBase, "esac" ) == 0 )
*type = tkn_esac;
else if( strcmp( bufBase, "for" ) == 0 )
*type = tkn_for;
else if( strcmp( bufBase, "while" ) == 0 )
*type = tkn_while;
else if( strcmp( bufBase, "until" ) == 0 )
*type = tkn_until;
else if( strcmp( bufBase, "do" ) == 0 )
*type = tkn_do;
else if( strcmp( bufBase, "done" ) == 0 )
*type = tkn_done;
else if( strcmp( bufBase, "break" ) == 0 )
*type = tkn_break;
else if( strcmp( bufBase, "continue" ) == 0 )
*type = tkn_continue;
else if( strcmp( bufBase, "exit" ) == 0 )
*type = tkn_exit;
else
#endif
if( isNumber )
*type = tkn_digit;
else
*type = tkn_identifier;
}
break;
}
*buf++ = '\0';
return( str );
}
/*******************************************************************/
void ExpandShellStr( WHandle ShellWh, char *string )
{
char *sp, *bp, *vp, *bep, buf[ 256 ], varBuf[ 64 ];
Boolean expanded = FALSE;
sp = string;
bp = buf;
bep = buf + 256;
while( *sp && (bp<bep))
{
if( *sp == '\'' ) /* Quote, pass all chars to next quote */
{
*bp++ = *sp++; /* save the quote */
while( *sp && (*sp != '\''))
*bp++ = *sp++;
*bp++ = *sp++; /* save the quote */
}
else if( *sp == '$' ) /* start of a shell var */
{
vp = varBuf;
sp++;
while( *sp && !isShellSpace(*sp) && !isSpecial(*sp) )
*vp++ = *sp++;
vp = ShellGetVar( ShellWh, varBuf );
if( *vp ) /* will always return a string, may not have any data */
{
while( *vp ) /* copy var to buf string */
*bp++ = *vp++;
expanded = TRUE;
}
else
{
*bp++ = '$'; /* re-enter the $ */
vp = varBuf;
while( *vp ) /* no var, copy back to buf string */
*bp++ = *vp++;
}
}
else
*bp++ = *sp++;
}
*bp = '\0';
if( expanded )
strcpy( string, buf ); /* copy the expanded string back */
}